package inference;

import basics.Vector;

public class MessagePassing {

	/**
	 * @param X
	 *            : The features of the uses
	 * @param Y
	 *            : The features of the items
	 * @param lDimension
	 *            : Dimension of the latent space
	 */
	public void run(Graph g) {
		for (GraphNode n : g.queryNodes()) {
			n.to(null);
			// calculateNode((GraphVariable) n);
		}
	}

	private void calculateNode(GraphVariable v) {
		Vector s = null;

		for (GraphEdge edge : v.edges().values()) {
			for (GraphNode n : edge.nodes()) {
				GraphFactor u = (GraphFactor) n;
				if (s == null) {
					// s = Vector.product(incomingMsgs(u, v), (Vector) u.to(v).getValue());
				} else {
					// s.add(Vector.product(incomingMsgs(u, v), (Vector) u.to(v).getValue()));
				}
			}
		}
	}

	private Vector incomingMsgs(GraphFactor u, GraphVariable v) {
		Vector p = null;
		for (GraphEdge e : v.edges().values()) {
			for (GraphNode f : e.nodes()) {
				if (f != u) {
					Vector t = incomingMsgs((GraphFactor) f, v);
					if (p == null) {
						p = t;
					} else {
						p.timesEqual(t);
					}
				}
			}
		}

		return p;
	}

}
